import 'package:boss/ui/widget/calendar/model/calendar_day.dart';

class DateUtils {
  //判断是否是同一天
  static bool sameDate(DateTime dateTimeOne, DateTime dateTimeTwo) {
    return dateTimeOne.year == dateTimeTwo.year &&
        dateTimeOne.month == dateTimeTwo.month &&
        dateTimeOne.day == dateTimeTwo.day;
  }

  //判断是否是同一月
  static bool sameMonth(DateTime dateTimeOne, DateTime dateTimeTwo) {
    return dateTimeOne.year == dateTimeTwo.year &&
        dateTimeOne.month == dateTimeTwo.month;
  }

  //1-12月天数
  static const List<int> _daysInMonth = const <int>[
    31,
    -1,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31
  ];

  //获取月有多少天
  static int getDaysInMonth(DateTime dateTime) {
    var year = dateTime.year;
    var month = dateTime.month;

    if (month == DateTime.february) {
      final bool isLeapYear =
          (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
      return isLeapYear ? 29 : 28;
    }
    return _daysInMonth[month - 1];
  }

  // 返回startDate到endDate中间的月基数
  static int monthDelta(DateTime startDate, DateTime endDate) {
    return (endDate.year - startDate.year) * 12 +
        endDate.month -
        startDate.month;
  }

  // 添加monthsToAdd月
  static DateTime addMonthsToMonthDate(DateTime monthDate, int monthsToAdd) {
    return DateTime(monthDate.year, monthDate.month + monthsToAdd);
  }

  // 返回startDate到endDate的年基数
  static int yearDelta(DateTime startDate, DateTime endDate) {
    return (endDate.year - startDate.year);
  }

  //当天的23:59
  static DateTime endOfTheDay(DateTime date) {
    return DateTime(date.year, date.month, date.day)
        .add(Duration(days: 1))
        .subtract(Duration(milliseconds: 1));
  }

  //当天的00:00
  static DateTime startOfTheDay(DateTime date) {
    return DateTime(date.year, date.month, date.day);
  }

  //上一个月
  static int prevMonth(DateTime dateTime) {
    var month = dateTime.month;
    if (month == 1) {
      return 12;
    }

    return month - 1;
  }

  //上一个月的天数
  static int getDaysInPrevMonth(DateTime dateTime) {
    return getDaysInMonth(DateTime(dateTime.year, prevMonth(dateTime)));
  }

  //获取月第一天和最后一天星期几
  static List<int> getWeekDayInMonthFirstAndLast(DateTime dateTime) {
    var firstDateTime = DateTime(dateTime.year, dateTime.month);
    var lastDateTime =
        DateTime(dateTime.year, dateTime.month, getDaysInMonth(dateTime));

    return [firstDateTime.weekday, lastDateTime.weekday];
  }

  //获取当前月份需要展示的日期数组
  static List<CalendarDay> getDays(DateTime dateTime, {DateTime minDateTime}) {
    List<int> completions = [1, 2, 3, 4, 5, 6, 0];

    var days = DateUtils.getDaysInMonth(dateTime);
    var firstAndLastWeekDay = DateUtils.getWeekDayInMonthFirstAndLast(dateTime);

    List<CalendarDay> calendarDays = [];
    //当前月份日期
    for (int i = 1; i <= days; i++) {
      var currentDateTime = DateTime(dateTime.year, dateTime.month, i);
      calendarDays.add(CalendarDay(
        day: '$i',
        isDisabled: minDateTime == null
            ? false
            : DateTime(minDateTime.year, minDateTime.month, minDateTime.day)
                .isAfter(currentDateTime),
        dateTime: currentDateTime,
      ));
    }
    //上月月份补全日期
    var prevMonthDays = DateUtils.getDaysInPrevMonth(dateTime);
    var prefixCount = completions[firstAndLastWeekDay[0] - 1];
    var prefixStartDay = prevMonthDays - prefixCount;
    List<CalendarDay> prefixDays = [];
    for (int i = 1; i <= prefixCount; i++) {
      prefixDays.add(CalendarDay(
        day: '${prefixStartDay + i}',
        isDisabled: true,
      ));
    }
    //下个月份补全日期
    var totalCount = prefixDays.length + calendarDays.length;
    var suffixCount = 7 - totalCount % 7;
    List<CalendarDay> suffixDays = [];

    if (suffixCount != 7)
      for (int i = 0; i < suffixCount; i++) {
        suffixDays.add(CalendarDay(
          day: '${i + 1}',
          isDisabled: true,
        ));
      }

    return [...prefixDays, ...calendarDays, ...suffixDays];
  }

  //获取当前星期第一天
  static DateTime startOfTheWeek(DateTime date) {
    var zeroDateTime = startOfTheDay(date);
    var weekday = date.weekday;
    return zeroDateTime.subtract(Duration(days: weekday - 1));
  }

  //获取当前星期最后一天
  static DateTime endOfTheWeek(DateTime date) {
    var start = startOfTheDay(date);
    return start.add(Duration(days: 6));
  }

  //获取当前月的第一天
  static DateTime startOfTheMonth(DateTime date) {
    return DateTime(date.year, date.month);
  }

  //获取当前月最后一天
  static DateTime endOfTheMonth(DateTime date) {
    var start = startOfTheMonth(date);
    var days = getDaysInMonth(date);
    return start.add(Duration(days: days)).subtract(Duration(seconds: 1));
  }

  //获取上一个月的第一天
  static DateTime startOfPrevMonth(DateTime date) {
    var temp = startOfTheMonth(date).subtract(Duration(days: 1));

    return startOfTheMonth(temp);
  }

  //获取上个月的最后一天
  static DateTime endOfPrevMonth(DateTime date) {
    var temp = startOfTheMonth(date).subtract(Duration(days: 1));

    return endOfTheMonth(temp);
  }
}
